from flask import Flask, render_template, request,redirect,url_for,session
from test import con_my_sql
app = Flask(__name__)
app.secret_key='123456'

@app.route("/")
def index_login():
    return render_template('login.html')

@app.route("/")
def logout():
    session.pop('username',None)
    return render_template('index_login')

@app.route("/register")
def index_register():
    return render_template('register.html')

@app.route("/index")
def index():
    if "username"in session:
       return render_template('index.html')
    else:
        return redirect(url_for("index_login"))

login_data = {
    "张三": "123456"
}

@app.route("/login", methods=["post"])
def login():
    name = request.form.get("username")
    pwd = request.form.get("password")

    code ="select * from login_user where username='%s'" %(name)
    cursor_ans=con_my_sql(code)
    cursor_select= cursor_ans.fetchall()

    if len(cursor_select)>0:
        if pwd == cursor_select[0]['password']:
            # return  "登陆成功"
            session["username"]=name
            return redirect(url_for("index"))
        else:
            return '密码错误 <a href="/">返回登录</a>'
    else:
        return '用户不存在<a href="/">返回登录</a>'

@app.route("/register", methods=["post"])
def register():
    name = request.form.get("username")
    pwd = request.form.get("password")

    code = "select * from login_user where username='%s'" % (name)
    cursor_ans = con_my_sql(code)
    cursor_select = cursor_ans.fetchall()
    if len(cursor_select) > 0:
         return '用户已存在<a href="/">返回登录</a>'
    else:
        code = "INSERT INTO `login_user` (`username`, `password`) VALUES ('%s','%s')" % (name, pwd)
        con_my_sql(code)
        return '注册成功<a href="/">返回登录</a>'


if __name__ == '__main__':
    app.run(port=5001)
    # code = "SELECT * FROM login_user "
    # cursor_ans = con_my_sql(code)    # 传递参数元组
    # print(cursor_ans.fetchall())
